package com.dt.common.validation.impl;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.dt.common.validation.exception.ValidationException;
import com.dt.common.validation.itf.PropertyGetter;

public class DefaultPropertyGetter implements PropertyGetter {

	@SuppressWarnings("unused")
	private boolean supportMapObject = false;
	private PropertyDescriptor propertyDescriptor;

	public DefaultPropertyGetter(PropertyDescriptor propertyDescriptor, boolean supportMapObject) {
		this.propertyDescriptor = propertyDescriptor;
		this.supportMapObject = supportMapObject;
	}

	@Override
	public Object get(Object bean) {
		Method method = propertyDescriptor.getReadMethod();
		if (!method.isAccessible()) {
			method.setAccessible(true);
		}

		try {
			return method.invoke(bean);
		} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			throw new ValidationException("exception while get bean property " + method.getName(), e);
		}
	}

}
